﻿using CodeGenerator.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeGenerator.UI
{
    public partial class FrmDict : Form
    {
        public FrmDict()
        {
            InitializeComponent();
        
        }

        private List<TableInfo> selectTable = null;
        private List<string> selectStruct=null;
        private string outputDir = string.Empty;
        string dictType = string.Empty;

        private void FrmDict_Load(object sender, EventArgs e)
        {
            ucSelectTableStruct1.Visible = false;
            ucSetting1.Visible = false;

            ChangeBtnState();
        }

        private void ChangeBtnState()
        {
            if (ucSelectTable1.Visible)
            {
                btnPrev.Enabled = false;
                btnNext.Enabled = true;
                btnOk.Enabled = false;
            }
            else if (ucSelectTableStruct1.Visible)
            {
                btnPrev.Enabled = true;
                btnNext.Enabled = true;
                btnOk.Enabled = false;
            }
            else if (ucSetting1.Visible)
            {
                btnPrev.Enabled = true;
                btnNext.Enabled = false;
                btnOk.Enabled = true;
            }
        }

        private void ChangeDisplayState(bool isPrev)
        {
            
            if (isPrev)
            {
                if (ucSelectTableStruct1.Visible)
                {
                    ucSelectTable1.Visible = true;
                    ucSelectTableStruct1.Visible = false;
                    ucSetting1.Visible = false;

                }
                else if (ucSetting1.Visible)
                {
                    ucSelectTable1.Visible = false;
                    ucSelectTableStruct1.Visible = true;
                    ucSetting1.Visible = false;
                }
            }
            else
            {
                if (ucSelectTable1.Visible)
                {
                    selectTable = ucSelectTable1.GetSelectTable;

                    if (selectTable.Count > 0)
                    {
                        ucSelectTable1.Visible = false;
                        ucSelectTableStruct1.Visible = true;
                        ucSetting1.Visible = false;
                    }
                    else
                    {
                        Common.MessageUtil.ShowWarning("请至少选择一个表！！！");
                    }

                }
                else if (ucSelectTableStruct1.Visible)
                {
                    selectStruct = ucSelectTableStruct1.GetSelectStruct;
                    if (selectStruct.Count > 0)
                    {
                        ucSelectTable1.Visible = false;
                        ucSelectTableStruct1.Visible = false;
                        ucSetting1.Visible = true;

                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.AppendFormat(string.Format("您选择的数据库是：{0}\r\n", Constant.DatabaseName), new object[0]);
                        stringBuilder.AppendFormat("\r\n您选择了以下表：\r\n", new object[0]);
                        foreach (var current in selectTable)
                        {
                            stringBuilder.AppendFormat("\t{0} \r\n", current.Name);
                        }
                        stringBuilder.AppendFormat("\r\n您选择了以下数据结构：\r\n", new object[0]);
                        foreach (string current in selectStruct)
                        {
                            stringBuilder.AppendFormat("\t{0} \r\n", current);
                        }
                        ucSetting1.SetInfo(stringBuilder);
                    }
                    else
                    {
                        Common.MessageUtil.ShowWarning("请至少选择一个类型！！！");
                    }

                }
            }
            ChangeBtnState();
        }

        #region 按钮组
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        } 
        private void btnPrev_Click(object sender, EventArgs e)
        {
            ChangeDisplayState(true);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
             ChangeDisplayState(false);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            outputDir = ucSetting1.GetOutputDir();

            if (string.IsNullOrEmpty(outputDir))
            {
                Common.MessageUtil.ShowWarning("请至少输出路径！！！");
                return;
            }

            dictType = ucSetting1.GetDictType();
            if (dictType == "Chm")
            {
                ucSetting1.GeneratorChm(selectTable, outputDir);

                btnOk.Enabled = false;
            }
            else if(dictType=="Html")
            {
                ucSetting1.GeneratorHtml(selectTable);
            }
            else if (dictType == "Excel")
            {
                ucSetting1.GeneratorExcel(selectTable);
            }
            else if(dictType == "Word")
            {
                ucSetting1.GeneratorWord(selectTable);
            }
            else
            {
                Common.MessageUtil.ShowWarning("正在开发中！！！");
            }
        }
        
        #endregion

        private void FrmDict_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ucSetting1.doWorkChm!=null && ucSetting1.doWorkChm.IsBusy)
            {
                Common.MessageUtil.ShowWarning("任务执行中，无法关闭！！！");
                e.Cancel = true;
            }
            else if (ucSetting1.doWorkHtml != null && ucSetting1.doWorkHtml.IsBusy)
            {
                Common.MessageUtil.ShowWarning("任务执行中，无法关闭！！！");
                e.Cancel = true;
            }
            else if (ucSetting1.doWorkExcel != null && ucSetting1.doWorkExcel.IsBusy)
            {
                Common.MessageUtil.ShowWarning("任务执行中，无法关闭！！！");
                e.Cancel = true;
            }
            else if (ucSetting1.doWorkWord != null && ucSetting1.doWorkWord.IsBusy)
            {
                Common.MessageUtil.ShowWarning("任务执行中，无法关闭！！！");
                e.Cancel = true;
            }
        }
    }
}
